<?php

namespace app\model;

use think\Model;
use think\facade\Log;

class Skill extends Model
{
    protected $table = 'skills';

    // 关联技能的项目
    public function project()
    {
        return $this->belongsTo(ProjectModel::class, 'project_id');
    }

    // 关联技能的成员
    public function members()
    {
        return $this->belongsToMany(MemberModel::class, 'member_skills', 'skill_id', 'member_id');
    }

    // 数据验证
    public static function validateData($data)
    {
        $validate = new \think\Validate([
            'name' => 'require|unique:skills|max:255',
            'description' => 'max:500',
        ]);

        return $validate->check($data);
    }

    // 获取技能的缓存
    public static function getSkillByIdWithCache($id)
    {
        $cacheKey = 'skill_' . $id;
        $skill = Cache::get($cacheKey);
        if (!$skill) {
            $skill = self::find($id);
            Cache::set($cacheKey, $skill, 3600);  // 缓存1小时
        }
        return $skill;
    }
}
